package edu.ynu.dao;
// default package

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;

import edu.ynu.po.CstLost;
import edu.ynu.po.Customer;

/**
 	* A data access object (DAO) providing persistence and search support for CstLost entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .CstLost
  * @author MyEclipse Persistence Tools 
 */

public class CstLostDAO extends BaseHibernateDAO  {
		 private static final Log log = LogFactory.getLog(CstLostDAO.class);
		//property constants
	public static final String CUSTOMER_ID = "customerId";
	public static final String LAST_ORDER_DATE = "lastOrderDate";
	public static final String LOST_DATE = "lostDate";
	public static final String REASON = "reason";
	public static final String STATUS = "status";



    
    public void save(CstLost transientInstance) {
        log.debug("saving CstLost instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(CstLost persistentInstance) {
        log.debug("deleting CstLost instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public CstLost findById( java.lang.Integer id) {
        log.debug("getting CstLost instance with id: " + id);
        try {
            CstLost instance = (CstLost) getSession()
                    .get("edu.ynu.po.CstLost", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(CstLost instance) {
        log.debug("finding CstLost instance by example");
        try {
            List results = getSession()
                    .createCriteria("edu.ynu.po.CstLost")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding CstLost instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from CstLost as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByCustomerId(Object customerId
	) {
		return findByProperty(CUSTOMER_ID, customerId
		);
	}
	
	public List findByLastOrderDate(Object lastOrderDate
	) {
		return findByProperty(LAST_ORDER_DATE, lastOrderDate
		);
	}
	
	public List findByLostDate(Object lostDate
	) {
		return findByProperty(LOST_DATE, lostDate
		);
	}
	
	public List findByReason(Object reason
	) {
		return findByProperty(REASON, reason
		);
	}
	
	public List findByStatus(Object status
	) {
		return findByProperty(STATUS, status
		);
	}
	
	public void lostCustomer(){
		
	}

	public List findAll(Map paramMap) {
		log.debug("finding all CstLost instances");
		try {
			String start = (String) paramMap.get("start");
			String limit = (String) paramMap.get("limit");
			String custNo = (String) paramMap.get("lstCstCustomer");
			String chargeMan = (String) paramMap.get("lstCustManagerName");
			String status = (String) paramMap.get("lstStatus");
			Criteria c = getSession().createCriteria(CstLost.class);
			if (StringUtils.isNotEmpty(custNo)) {
				c.add(Expression.like("custNo", "%" + custNo + "%"));// 编号
			}
			if(StringUtils.isNotEmpty(chargeMan)){
				c.createAlias("edu.ynu.po.Customer", "cust")
				.add(Expression.like("cust.chargeMan", "%"+chargeMan+"%"))
				.add(Expression.eqProperty("custNo", "cust.custNo"));
			}
			if(StringUtils.isNotEmpty(status)){
				c.add(Expression.like("status", "%"+status+"%"));
			}
			return c.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public CstLost merge(CstLost detachedInstance) {
        log.debug("merging CstLost instance");
        try {
            CstLost result = (CstLost) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(CstLost instance) {
        log.debug("attaching dirty CstLost instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(CstLost instance) {
        log.debug("attaching clean CstLost instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}